<?php
namespace app\common\server;
use x\Xbase;
class Goods extends Xbase
{
    /**
     * 添加商品
     * @param [type] $d [商品数据]
     */
    public function add($d)
    {
        $spec = htmlspecialchars_decode($d['spec']);
        $spec = json_decode($spec,true);
        if(!$spec){
            $spec = ['规格'=>[$d['title']]];
        }
        $spec_temp = $spec;
        asort($spec);
        $spec = json_encode($spec,JSON_UNESCAPED_UNICODE );
        $data = [
            'title' => $d['title'],
            'keyword' => $d['keyword'],
            'description' => $d['description'],
            'price' => $d['price'],
            'pic' => $d['pic'],
            'pics' => $d['pics'],
            'content' => isset($d['content']) ? $d['content'] : '',
            'attr' => $d['attr'],
            'spec' => htmlspecialchars($spec),
            'create_time' => time(),
            'cat_id' => $d['cat_id'],
            'shop_id' => $d['shop_id'],
            'status' => $d['status'],
            'postage' => $d['postage'],
            'changjia_info' => $d["changjia_info"],
            'point' => $d["point"],
            'fanyong' => $d["fanyong"]
        ];
        //添加商品
        $this->db->begin();
        $mysql = [];
        $mysql[] = $id = $this->db->insert("goods", $data);
        m('Attachment','xcom')->imgSure('goods',$id,$data);
        //添加规格表数据，产品价格表goods_price
        $attr = $spec_temp;
        foreach ($attr as $k => $v) {
            $attr[$k] = implode(',', $v);
        }
        $temp = m('GoodsPrice', 'c')->CountAttr($attr);
        $arr = $temp['arr'];
        $goods_price = [];
        foreach ($arr as $title) {
            $stock = isset($d[$title . '_stock'][0]) ? (int)$d[$title . '_stock'][0] : 100;
            $price = isset($d[$title . '_price'][0]) ? (float)$d[$title . '_price'][0] : $data['price'];
            $shop_no = isset($d[$title . '_shop_no'][0]) ? $d[$title . '_shop_no'][0] : "";
            $shop_num = isset($d[$title . '_shop_num'][0]) ? $d[$title . '_shop_num'][0] : "";
            
            if($shop_num!='' && $this->db->query("select * from goods_price where shop_num = ?",[$shop_num])){//验证条形码
                return $this->setError("条形码已存在");
            }
            $tmp = [
                'goods_id' => $id,
                'title' => $title,
                'stock' => $stock,
                'price' => $price,
                'shop_no' => $shop_no,
                'shop_num' => $shop_num
            ];
            $goods_price[] = $tmp;
        }
        $mysql[] = $this->db->insertAll("goods_price", $goods_price);
        return $this->db->end($mysql);
    }


    /**
     * 编辑商品
     * @param [type] $d [商品数据]
     */
    public function edit($d)
    {
        $spec = htmlspecialchars_decode($d['spec']);
        $spec = json_decode($spec,true);
        if(!$spec){
            $spec = ['规格'=>[$d['title']]];
        }
        $spec_temp = $spec;
        asort($spec);
        $spec = json_encode($spec,JSON_UNESCAPED_UNICODE );
        $data = [
            'title' => $d['title'],
            'keyword' => $d['keyword'],
            'description' => $d['description'],
            'price' => $d['price'],
            'pic' => $d['pic'],
            'pics' => $d['pics'],
            'content' => isset($d['content']) ? $d['content'] : '',
            'spec' => htmlspecialchars($spec),
            'attr' => $d['attr'],
            'update_time' => time(),
            'cat_id' => $d['cat_id'],
            'shop_id' => $d['shop_id'],
            'id' => $d['id'],
            'postage' => $d['postage'],
            'changjia_info' => $d["changjia_info"],
            'point' => $d["point"],
            'fanyong' => $d["fanyong"]
        ];
        if(isset($d['status'])){
          $data['status'] = $d['status'];
        }
        //添加商品
        $this->db->begin();
        $mysql = [];
        $mysql[] = $this->db->update("goods", $data);
        m('Attachment','xcom')->imgSure('goods',$data['id'],$data);
        //添加规格表数据，产品价格表goods_price
        $attr = $spec_temp;
        foreach ($attr as $k => $v) {
            $attr[$k] = implode(',', $v);
        }
        $temp = m('GoodsPrice', 'c')->CountAttr($attr);
        $arr = $temp['arr'];
        $ids = [];
        foreach ($arr as $title) {
            $stock = isset($d[$title . '_stock'][0]) ? (int)$d[$title . '_stock'][0] : 100;
            $price = isset($d[$title . '_price'][0]) ? (float)$d[$title . '_price'][0] : $data['price'];
            $shop_no = isset($d[$title . '_shop_no'][0]) ? $d[$title . '_shop_no'][0] : "";
            $shop_num = isset($d[$title . '_shop_num'][0]) ? $d[$title . '_shop_num'][0] : "";
            $tmp = [
                'goods_id' => $d['id'],
                'title' => $title,
                'stock' => $stock,
                'price' => $price,
                'shop_no' => $shop_no,
                'shop_num' => $shop_num,
                'update_time' => time(),
            ];
            $info = $this->db->query('select * from goods_price where  goods_id=? and title=?', [$data['id'], $title], 1);
            if ($info) {
                if($shop_num!='' && $this->db->query("select * from goods_price where shop_num = ? and id != ?",[$shop_num,$info['id']])){//验证条形码
                    return $this->setError("条形码已存在");
                }
                $tmp['id'] = $info['id'];
                $mysql[] = $this->db->update('goods_price', $tmp);
                $ids[] = $info['id'];
            } else {
                if($shop_num!='' && $this->db->query("select * from goods_price where shop_num = ?",[$shop_num])){//验证条形码
                    return $this->setError("条形码已存在");
                }
                $mysql[] = $id = $this->db->insert('goods_price', $tmp);
                $ids[] = $id;
            }
        }

        //删除多余的规格
        if($ids){
            $ids = implode(",", $ids);
            $this->db->query("delete from goods_price where id NOT IN (" . $ids . ") AND goods_id=?", [$data['id']]);
        }
        return $this->db->end($mysql);

    }

}
